package cyf.Owner.controller;

import cyf.Owner.bean.CarVo;
import cyf.Owner.bean.OwnerVo;
import cyf.Owner.service.OwnerService;
import cyf.Tools.ServerResponse;
import cyf.Tools.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.O;

/**
 * 业主与车辆相关controller层
 */
@Controller
@RequestMapping("/manage")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    /**
     * 跳转业主新增页面
     * @return
     */
    @RequestMapping("/index")
    public String indexpage(){
        return "owner";
    }

    /**
     * 跳转业主查询页面
     * @return
     */
    @RequestMapping("ownermessage")
    public String ShowOwner(){
        return "OwnerMessage";
    }

    /**
     * 跳转车辆录入页面
     * @return
     */
    @RequestMapping("indexCar")
    public String indexCar(){
        return "car";
    }

    /**
     * 跳转车辆查询页面
     * @return
     */
    @RequestMapping("ownercarmessage")
    public String OwnerCar(){
        return "OwnerCarMessage";
    }

    /**
     * 分页获取所有的业主信息
     * @param yzxm
     * @param yzzjhm
     * @param pageCurrent
     * @return
     * @throws Exception
     */
    @RequestMapping("getOwnerMessage")
    @ResponseBody
    public Map<String,Object> GetOwners(String yzxm, String yzzjhm, Integer pageCurrent) throws Exception {
        Map<String,Object > param = new HashMap<>();
        param.put("owner_name",yzxm);
        param.put("owner_zjhm",yzzjhm);
        Map<String,Object> map = ownerService.getAllOwner(param,pageCurrent);
        return map ;
    }

    /**
     * 插入一个新的业主信息
     * @param owner
     * @return
     */
    @RequestMapping("insertOwner")
    @ResponseBody
    public Map<String,Object> InsertOwner(@ModelAttribute OwnerVo owner){
       System.out.print(owner.toString());
       if(owner.getOwner_sex()!=null && !("".equals(owner.getOwner_sex())) && "1".equals(owner.getOwner_sex())){
           owner.setOwner_sex("女");
       }else if(owner.getOwner_sex()!=null && !("".equals(owner.getOwner_sex())) && "0".equals(owner.getOwner_sex())){
           owner.setOwner_sex("男");
       }
       Map<String,Object> result = ownerService.insertOwner(owner);
       return result;
    }

    /**
     * 插入一个新的车辆信息
     * @param car
     * @return
     */
    @RequestMapping("insertCar")
    @ResponseBody
    public Map<String,Object> InsertCar(@ModelAttribute CarVo car){
        System.out.print(car.toString());
        Map<String,Object> c_result = new HashMap<>();
        c_result.put("emptyflag",0);
        if(StringHelper.isEmpty(car.getCar_owner_id())){
            c_result.put("empty","请填写业主身份证号码");
            c_result.put("emptyflag",1);
            return c_result;
        }
        if(StringHelper.isEmpty(car.getCar_plate())){
            c_result.put("empty","请填写车牌号码");
            c_result.put("emptyflag",1);
            return c_result;
        }
        if(StringHelper.isEmpty(car.getCar_type())){
            c_result.put("empty","请填写车辆类型");
            c_result.put("emptyflag",1);
            return c_result;
        }
        Map<String,Object> result = ownerService.insertCar(car);
        return result;
    }

    /**
     * 分页查询车辆列表（带车主信息）
     * @param yzxm
     * @param yzzjhm
     * @param car_plate
     * @param pageCurrent
     * @throws Exception
     */
    @RequestMapping("getCarsByOwner")
    @ResponseBody
    public Map<String,Object> GetCarsByOwner(String yzxm, String yzzjhm,String car_plate, Integer pageCurrent) throws Exception {
        Map<String,Object > param = new HashMap<>();
        param.put("owner_name",yzxm);
        param.put("owner_zjhm",yzzjhm);
        param.put("car_plate",car_plate);
        Map<String,Object> map = ownerService.getCarsByOwner(param,pageCurrent);
        return map ;
    }

    @RequestMapping("deleteCarByPlate")
    @ResponseBody
    public Map<String,Object> deleteCarByPlate(String car_id){
        if(StringHelper.isEmpty(car_id)){
            Map<String,Object> result = new HashMap<>();
            result.put("success",false);
            result.put("mes","系统错误");
            return result;
        }
        Map<String,Object > param = new HashMap<>();
        param.put("car_id",car_id);
        Map<String,Object> map = ownerService.deleteCarByPlate(param);
        return map;
    }

    @RequestMapping("deleteOwnerBySfz")
    @ResponseBody
    public Map<String,Object> deleteOwnerBySfz(String owner_zjhm){
        if(StringHelper.isEmpty(owner_zjhm)){
            Map<String,Object> result = new HashMap<>();
            result.put("success",false);
            result.put("mes","系统错误");
            return result;
        }
        Map<String,Object > param = new HashMap<>();
        param.put("owner_zjhm",owner_zjhm);
        Map<String,Object> map = ownerService.deleteOwner(param);
        return map;
    }

}
